var wx_share = {};
(function(share) {
	share.currentIndex=0;
	share.localIds=[];

	share.startRecord = function(){
		wx.startRecord();
	},
	share.stopRecord = function(callback){
		wx.stopRecord({
			success: function (res) {
				callback&&callback(res.localId);
			}
		});
	},
	share.onVoiceRecordEnd = function(callback){
		wx.onVoiceRecordEnd({
			complete: function (res) {
				callback&&callback(res.localId);
			}
		});
	},
	share.uploadVoice=function(localId,callback){
		var customerId = $.util.getParam("customerId");
		$.util.pageLoading.show("上传中");
		wx.uploadVoice({
			localId: localId,
			success: function (res) {
				var serverId = res.serverId; // 返回音频的服务器
				$.req.get("Upload/uploadWxVoiceToOss.json?customerId="+customerId+"&d=" + (+new Date()), {
					"mediaId": serverId
				}, function (data) {
					$.util.pageLoading.hide();
					if (!data.code) {
						callback&&callback(data);
					} else {
						$.util.alert( data.msg);
					}
				})
			}
		})
	},
	share.playVoice=function(localId){
		wx.playVoice({
			localId: localId
		});
	},
	share.pauseVoice=function(localId){
		wx.pauseVoice({
			localId: localId
		});
	},
	share.onVoicePlayEnd = function(callback){
		wx.onVoicePlayEnd({
			success: function (res) {
				callback&&callback();
			}
		});
	}
	share.showImg=function(imgs,index){
		wx.previewImage({
			current: index, // 当前显示图片的http链接
    		urls: imgs // 需要预览的图片http链接列表
		});
	},
	share.uploadImage = function(callback) {
		var customerId = $.util.getParam("customerId");
		var me=this;
		wx.chooseImage({
			success: function(res) {
				var localIds = res.localIds;
				var status = {
					len: localIds.length
				};
				status.callback = callback;
				me.localIds=localIds;
				share.currentIndex=0;
				if (localIds && localIds.length > 0) {
					setTimeout(function() {
						me.uploadSingle(0,status);
					}, 150);
				} else {
					$.util.alert("请选择一张图片上传");
				}
			}
		});
	},
	share.uploadSingle=function(index,status) {
		var me=this;
		if(me.localIds.length>1) {
			$.util.pageLoading.show("正在上传第"+(me.currentIndex+1)+"张");
		} else {
			$.util.pageLoading.show("上传中");
		}
		wx.uploadImage({
			localId: me.localIds[index], // 需要上传的图片的本地ID，由chooseImage接口获得
			isShowProgressTips: 0, // 默认为1，显示进度提示
			success: function(res) {
				status.index = index;
				share.__upload(res, status,function(){
					if(me.localIds.length>0) {
						if(me.currentIndex>=me.localIds.length-1) {
							$.util.pageLoading.hide();
						}
					} else {
						$.util.pageLoading.hide();
					}
					if(me.currentIndex < me.localIds.length-1) {
						me.uploadSingle(++me.currentIndex,status);
					}
				});
			}
		});
	}
	share.uploadMultImage = function(callback) {
		var customerId = $.util.getParam("customerId");
		var me=this;
		wx.chooseImage({
			success: function(res) {
				var localIds = res.localIds;
				var status = {
					len: localIds.length
				};
				status.callback = callback;
				share.currentIndex=0;
				me.localIds=localIds;
				if (localIds && localIds.length > 0) {
					setTimeout(function() {
						me.uploadSingle(me.currentIndex,status);
					}, 150);
				} else {
					$.util.alert("请选择一张图片上传");
				}
			}
		});
	}
	share.__upload = function(res, status,uploadCallback) {
		var serverId = res.serverId; // 返回图片的服务器端ID
		var me=this;
		var customerId=$.util.getParam("customerId");
		$.req.get("Upload/uploadToOss.json?customerId="+customerId+"&d=" + (+new Date()), {
			"mediaId": serverId
		}, function(data) {
			if (!data.code) {
				uploadCallback && uploadCallback();
				status.callback && status.callback(data, status);
			}
		});
	}
	share.init = function(config) {
		var me = this;
		var pkg = config.pkg;
		var apiList = [
			'onMenuShareTimeline',
			'onMenuShareAppMessage',
			'onMenuShareQQ',
			'onMenuShareWeibo',
			'showMenuItems',
			'hideMenuItems',
			'chooseImage',
			'uploadImage',
			'previewImage',
			'downloadImage',
			'closeWindow',
			'hideAllNonBaseMenuItem',
			'showAllNonBaseMenuItem'
		];
		if (config.apiList) {
			apiList =apiList.concat( config.apiList );
		}
		wx.config({
			debug: false,
			appId: pkg.appId,
			timestamp: pkg.timestamp,
			nonceStr: pkg.nonceStr,
			signature: pkg.signature,
			jsApiList: apiList
		});
		var baseMenuItems = [
			"menuItem:share:appMessage",
			"menuItem:share:timeline",
			"menuItem:share:weiboApp",
			"menuItem:share:facebook",
			"menuItem:share:qq",
			"menuItem:share:QZone",
			"menuItem:copyUrl"
		];
		wx.ready(function() {
			if (config.title) {
				if (!config.hide) {
					wx.showMenuItems({
						menuList: baseMenuItems
					});
				}
				wx.onMenuShareAppMessage({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "", // 分享图标,
					type: config.type || "",
					success: function() {
						config.success && config.success(config, 1);
					},
					cancel: function() {
						config.cancel && config.cancel(config, 1);
					}
				});
				wx.onMenuShareTimeline({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "", // 分享图标,
					type: config.type || "",
					success: function() {
						config.success && config.success(config, 2);
					},
					cancel: function() {
						config.cancel && config.cancel(config, 2);
					}
				});
				wx.onMenuShareQQ({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "", // 分享图标,
					type: config.type || "",
					success: function() {
						config.success && config.success(config, 3);
					},
					cancel: function() {
						config.cancel && config.cancel(config, 3);
					}
				});
				wx.onMenuShareWeibo({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "", // 分享图标,
					type: config.type || "",
					success: function() {
						config.success && config.success(config, 4);
					},
					cancel: function() {
						config.cancel && config.cancel(config, 4);
					}
				});
			}
			if (config.hide) {
				wx.hideMenuItems({
					menuList: baseMenuItems
				});
			}
			if(config.hideAllnone)
			{
				wx.hideAllNonBaseMenuItem();
			}
			if(!config.hideAllnone)
			{
				wx.showAllNonBaseMenuItem();
			}
		});
	}
})(wx_share);